package com.heart.house.api.dao;

import com.google.common.collect.Lists;
import com.heart.house.api.common.RestCode;
import com.heart.house.api.common.RestResponse;
import com.heart.house.api.config.GenericRest;
import com.heart.house.api.model.Agency;
import com.heart.house.api.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Luo Zhen
 * @create 2019-07-18 11:54
 */
@Repository
public class UserDao {

    @Autowired
    private GenericRest rest;
    @Value("${user.service.name}")
    private String userServiceName;

    public List<User> getUserList(User query) {
        String url = "http://" + userServiceName + "/user/getList";
        ResponseEntity<RestResponse<List<User>>> resultEntity = rest.post(url, query, new ParameterizedTypeReference<RestResponse<List<User>>>() {
        });
        RestResponse<List<User>> restResponse = resultEntity.getBody();
        // 判断状态码
        if (RestCode.OK.code == restResponse.getCode()) {
            return restResponse.getResult();
        } else {
            return Lists.newArrayList();
        }
    }

    public User addUser(User account) {
        String url = "http://" + userServiceName + "/user/add";
        ResponseEntity<RestResponse<User>> resultEntity = rest.post(url, account, new ParameterizedTypeReference<RestResponse<User>>() {
        });
        RestResponse<User> restResponse = resultEntity.getBody();
        // 判断状态码
        if (RestCode.OK.code == restResponse.getCode()) {
            return restResponse.getResult();
        }
        throw new IllegalStateException("Can not add User");
    }

    public boolean enable(String key) {
        String url = "http://" + userServiceName + "/user/enable?key=" + key;
        RestResponse<Object> response = rest.get(url, new ParameterizedTypeReference<RestResponse<Object>>() {
        }).getBody();
        return RestCode.OK.code == response.getCode();
    }

    public User authUser(User user) {
        String url = "http://" + userServiceName + "/user/auth";
        ResponseEntity<RestResponse<User>> responseEntity = rest.post(url, user, new ParameterizedTypeReference<RestResponse<User>>() {
        });
        RestResponse<User> response = responseEntity.getBody();
        if (RestCode.OK.code == response.getCode()) {
            return response.getResult();
        }
        throw new IllegalStateException("Can not auth User");
    }

    public void logout(String token) {
        String url = "http://" + userServiceName + "/user/logout?token=" + token;
        rest.get(url, new ParameterizedTypeReference<RestResponse<Object>>() {
        });
    }

    public User getUserByToken(String value) {
        String url = "http://" + userServiceName + "/user/get?token=" + value;
        RestResponse<User> response = rest.get(url, new ParameterizedTypeReference<RestResponse<User>>() {
        }).getBody();
        if (response == null || RestCode.OK.code == response.getCode()) {
            return null;
        }
        return response.getResult();
    }

    public List<Agency> getAllAgency() {
        return null;
    }

    public User updateUser(User updateUser) {
        return null;
    }
}
